package pers.csw.lookBook.controller.front;


import cn.dev33.satoken.annotation.SaCheckLogin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import pers.csw.lookBook.common.Res;
import pers.csw.lookBook.entity.ReadHistory;
import pers.csw.lookBook.service.ReadHistoryService;

import java.util.Date;
import java.util.List;


@CrossOrigin
@RestController
@RequestMapping("/read-history")
public class ReadHistoryController {
    @Autowired
    ReadHistoryService readHistoryService;

    /**
     * 通过用户id和书籍id查询阅读历史
     * @param userId 用户id
     * @param bookId 书籍id
     * @return 返回该用户下该书籍的阅读历史
     */
    @SaCheckLogin
    @GetMapping("/getReadHistoryByUserIdAndBookId/{userId}/{bookId}")
    public Res getReadHistoryByUserIdAndBookId(@PathVariable Integer userId,@PathVariable Integer bookId){
        ReadHistory readHistoryByUserIdAndBookId = readHistoryService.getReadHistoryByUserIdAndBookId(userId, bookId);
        return Res.success(readHistoryByUserIdAndBookId);
    }

    /**
     * 通过用户id查询该用户所有的阅读历史
     * @param userId 用户id
     * @return 返回该用户的所有阅读历史
     */
    @SaCheckLogin
    @GetMapping("/getReadHistoryListByUserId/{userId}")
    public Res getReadHistoryListByUserId(@PathVariable Integer userId){
        return Res.success( readHistoryService.getReadHistoryList(userId));
    }


    /**
     * 通过阅读类来修改历史记录
     * @param readHistory 阅读类
     * @return 返回是否成功
     */
    @SaCheckLogin
    @PutMapping("/updateReadHistory")
    public Res updateReadHistory(@RequestBody ReadHistory readHistory){
        readHistory.setUpdateTime(new Date());
        boolean b = readHistoryService.updateById(readHistory);
        return Res.success(b);
    }

    /**
     * 阅读历史
     * @param readHistory 阅读历史对象
     * @return 返回是否添加成功
     */
    @SaCheckLogin
    @PostMapping("insertReadHistory")
    public Res insertReadHistory(@RequestBody ReadHistory readHistory){
        readHistory.setCreateTime(new Date());
        boolean save = readHistoryService.save(readHistory);
        return Res.success(save);
    }

    /**
     * 通过id删除记录
     * @param id 阅读历史记录id
     * @return 返回是否成功
     */
    @SaCheckLogin
    @DeleteMapping("deleteReadHistory/{id}")
    public Res deleteReadHistory(@PathVariable Integer id){
        boolean b = readHistoryService.removeById(id);
        return Res.success(b);
    }
}
